<template>
    <div>
        <h1>医生新增</h1>
        <table class="table table-bordered" style="background-color:lightcoral ;">
            <tbody>
                <tr>
                    <td>姓名</td>
                    <td><input type="text" v-model="info.name"></td>
                </tr>
                  <tr>
                    <td>头像</td>
                    <td><input type="file" @change="img">
                        <img :src="info.img" width="60px" height="60px">
                    </td>
                </tr>
                <tr>
                    <td>科室</td>
                    <td>  <select v-model="info.oid">
            <option value="">请选择</option>
            <option :value="item.oid" v-for="item in level" :key="item.oid">{{item.oname}}</option>  
        </select></td>
                </tr>
                <tr>
                    <td>创建日期</td>
                    <td><input type="date" v-model="info.createTime"></td>
                </tr>
                 <tr>
                    <td>第一执业医院</td>
                    <td><input type="text" v-model="info.hospital"></td>
                </tr>
                   <tr>
                    <td>职称</td>
                    <td><input type="text" v-model="info.work"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import {ref,onMounted} from 'vue'

let info:any=ref({
  "id": 0,
  "name": "",
  "img": "",
  "work": "",
  "hospital": "",
  "createTime": "",
  "oid": ""
})

let level:any=ref([]);

onMounted(()=>{
    Getlevel();
})

  const Getlevel=()=>{
     axios({
        url:'https://localhost:7292/api/Office/GetLevelOffcie',
        method:'get',
     })
     .then((res)=>{
        console.log(res);
        level.value=res.data
     })
     .catch((err)=>{
        console.log(err);
     })
  }
  const add=()=>{
    if(info.value.name==""){
        alert("姓名不能为空");
        return;
    }
     if(info.value.hospital==""){
        alert("第一执业医院不能为空");
        return;
    }
     if(info.value.work==""){
        alert("职称不能为空");
        return;
    }
    axios({
        url:'https://localhost:7292/api/Doctor/AddDoctor',
        method:'post',
        data:info.value
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1){
            alert("名称不能相同")
            return;
        }
        alert("添加成功")
         location.href='/showDoctor'
    })
  }

  const img=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData();
    f.append('file',obj);
    axios({
        url:'https://localhost:7292/api/Img/Img',
        method:'post',
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="不是图片"){
            alert("您上传的不是图片");
            return;
        }
        if(res.data=="图片大于2M"){
            alert("上传的图片不能大于2M");
            return;
        }
        info.value.img=res.data
    })
}
</script>

<style scoped>

</style>